<?php
namespace App\Http\Controllers\Admin;

use Session,Redirect;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Admin\ThrottlesLogins;
use Illuminate\Foundation\Admin\AuthenticatesAndRegistersUsers;
use DB;
class RecommendController extends Controller
{
	//推荐广告图列表
	public function Recommend_list(){
		$Recommend = new \App\Recommend();
		$list = $Recommend->select();
		return view("Admin/recommend/Recommend_list",["recommend"=>$list]);
	}
	//推荐广告图添加
	public function Recommend_add(){
		$req = request();
		if($_POST){
			 $Recommend = new \App\Recommend();
			 $pic = str_random(3).$_FILES['image']['name'];
			 $req->file('image')->move('admin/recommend',$pic);
	          $data = array(
	              'name'=>$req->names,
	              'url'=>$req->url,
	              'image'=>'admin/recommend/'.$pic,
	              'type'=>1,
	              'is_show'=>1
	          );
	        $res = $Recommend->insert($data);
	        if($res){
			 	return redirect('/admin/Recommend_list');
			 }else{
			 	echo '<script>alert("添加广告图失败");window.location.href="Recommend_add";</script>';
			 } 
		}else{
			return view('Admin/recommend/Recommend_add');
		}
	}
	//推荐广告图修改
	public function Recommend_up($adv_id){
		$req = request();
		$recommend = new \App\Recommend();
		if($_POST){
			if(!empty($_FILES['image']['name'])){
				 $pic = str_random(3).$_FILES['image']['name'];
		 		 $req->file('image')->move('admin/recommend',$pic);
			}else{
				 $pic = $_POST["images"];
			}
          	$data = array(
              'name'=>$req->names,
              'url'=>$req->url,
              'image'=>'admin/recommend/'.$pic
          	);
            $res = $recommend->lists_up($data,$adv_id);
            if($res == 0){
                echo '<script>alert("修改轮播图失败");window.location.href="Recommend_up";</script>';
            }else{
                return redirect('/admin/Recommend_list');
            }
		}else{
			$res = $recommend->getOne($adv_id);
			return view("Admin/recommend/Recommend_up",['recommend'=>$res]);
		}
	}
	//推荐广告图删除
	public function Recommend_del($adv_id){
		$recommend = new \App\Recommend();
		$is_show = $recommend->getOne($adv_id);
		if($is_show->is_show == 0){
			$data = ['is_show'=>1];
		}else{
			$data = ['is_show'=>0];
		}
        $res = $recommend->lists_del($adv_id,$data);
        var_dump($res);
        if($res != 0){
        	return redirect('/admin/Recommend_list');
        }
	}
	//推荐广告图
	public function Recommend_advert($adv_id){
		$recommend = new \App\Recommend();
		if(!empty($_POST)){
			$data = array("level"=>$_POST["adv"]);
			$res = $recommend->lists_up($data,$adv_id);
			if($res == 0){
                echo '位置推荐失败';
            }else{
                return redirect('/admin/Recommend_list');
            }
		}else{
			$res = $recommend->getOne($adv_id);
			return view("Admin/recommend/Recommend_advert",['recommend'=>$res]);
		}
	}
}

?>